<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        list-style: none;
      }

      .wrap {
        width: 700px;
        min-height: 500px;
        background-color: rgb(99, 213, 245);
        background-repeat: no-repeat;
        background-size: cover;
        margin: 0 auto;
        position: relative;
        overflow: hidden;
      }

      .wrap #form {
        width: 460px;
        height: 120px;
        background: #fff;
        border-radius: 12px;
        position: absolute;
        left: 50%;
        top: 116px;
        transform: translateX(-50%);
        display: flex;
        flex-wrap: wrap;
        padding: 26px;
        box-sizing: border-box;
      }

      .wrap #form p {
        margin-bottom: 10px;
      }

      .wrap #form p input {
        width: 140px;
        height: 28px;
        border-radius: 12px;
        border: solid 1px #19c2ff;
        margin: 0 10px;
        padding: 5px;
        box-sizing: border-box;
      }

      .wrap #form p:last-child input {
        width: 70px;
        background: #19c2ff;
        color: #fff;
        outline: none;
      }

      .wrap #form p select {
        width: 140px;
        height: 28px;
        border-radius: 12px;
        border: solid 1px #19c2ff;
        margin: 0 5px;
      }

      .wrap #table {
        width: 460px;
        border-radius: 12px;
        position: absolute;
        left: 50%;
        top: 275px;
        transform: translateX(-50%);
        box-sizing: border-box;
        overflow: hidden;
      }

      .wrap #table thead {
        width: 100%;
        height: 30px;
        line-height: 30px;
        text-align: center;
        border-top-left-radius: 12px;
        border-top-right-radius: 12px;
        color: #fff;
        background: #4cbfe5;
      }

      .wrap #table thead a {
        color: #fff;
      }

      .wrap #table tbody {
        color: #19c2ff;
      }

      .wrap #table tbody a {
        color: #19c2ff;
      }

      .wrap #table tbody tr {
        background: #fff;
      }

      .wrap #table tbody tr th:nth-child(1) {
        text-align: left;
        padding-left: 20px;
      }

      /* input[type='checkbox'] {
    width: 12px;
    height: 12px;
    background-color: #fff;
     -webkit-appearance: none; 
    border: 1px solid #19c2ff;
    border-radius: 2px;
    outline: none;
}

input[type=checkbox]:checked {
    color: #19c2ff;
} */
      .title {
        width: 100%;
        height: 200px;
        margin-top: 40px;
        text-align: center;
        font-size: 30px;
        color: white;
      }
    </style>
  </head>
  <body>
    <div class="wrap">
      <div class="title">学生系统</div>
      <div id="form">
        <p>姓名<input id="username" type="text" value="张三123" /></p>
        <p>年龄<input id="age" type="text" value="21" /></p>
        <p>
          性别
          <select id="gender">
            <option>男</option>
            <option>女</option>
            <option>你猜</option>
          </select>
        </p>
        <p>
          <input type="button" value="添加" id="addBtn" />
          <input type="button" value="排序" id="sortBtn" />
        </p>
      </div>
      <table id="table">
        <thead>
          <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody></tbody>
      </table>
    </div>
    <script>
      var username = document.getElementById("username");
      var age = document.getElementById("age");
      var gender = document.getElementById("gender");
      var addBtn = document.getElementById("addBtn");
      var sortBtn = document.getElementById("sortBtn");
      var tbody = document.getElementById("table").lastElementChild;
      addBtn.onclick = function () {
        var obj = {
          name: username.value,
          age: age.value,
          gender: gender.value,
        };
        // console.log(obj);
        var tr = document.createElement("tr");
        for (var key in obj) {
          var td = document.createElement("td");
          td.innerHTML = obj[key];
          tr.appendChild(td);
        }
        var del = document.createElement("td");
        del.innerHTML = "删除";
        tr.appendChild(del);
        del.onclick = function () {
          tr.remove();
        };
        tbody.appendChild(tr);
      };

      // 排序

      sortBtn.onclick = function () {
        var trs = tbody.children;
        trs = [...trs];
        console.log(trs);
        var arr = [];
        trs.forEach(function (item, key) {
          var ages = item.children[1].innerHTML;
          console.log(ages);
          arr.push(ages);
        });
        arr = arr.sort(function (a, b) {
          return a - b;
        });
        arr.forEach(function (a, k) {
          trs.forEach(function (item, key) {
            if (a === item.children[1].innerHTML) {
              tbody.appendChild(item);
            }
          });
        });
      };
    </script>
  </body>
</html>
